package ihm.actions;

import ihm.models.ATKmodel;
import ihm.views.main.catalog.FilterBar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import systems.database.query.condition.ComparatorEnum;
import systems.database.query.condition.Condition;
import systems.database.query.condition.Expression;
import systems.database.query.condition.JunctionEnum;
import systems.database.query.condition.Literal;
import systems.rpgBuilder.RpgBuilder;
import systems.rpgBuilder.Type;

public class FilterAction implements ActionListener{

  private ATKmodel model;
  private Condition filterCondition = new Condition();
  private FilterBar filterBar;

  public FilterAction(FilterBar filterBar, ATKmodel model)
  {
    this.filterBar = filterBar;
    this.model = model;
  }

  @Override
  public void actionPerformed(ActionEvent e)
  {
    this.filterCondition = new Condition();

    String[] values = model.getSelectedItem().getTableFieldValues();
    Literal itemField, itemValue;
    int factionId;
    int typeId;
    int minLvl, maxLvl;
    factionId = filterBar.getFactionSelected();
    typeId = filterBar.getTypeSelected();
    minLvl = filterBar.getMinLevel();
    maxLvl = filterBar.getMaxLevel();

    Expression exp1;
    itemField = new Literal(this.model.getSelectedItem().getTableField());

    for(String val : values)
    {
      exp1 = new Expression(itemField);
      itemValue = new Literal(val.trim());
      exp1.addComparatorAndLiteral(ComparatorEnum.LIKE, itemValue);
      if(this.filterCondition.getExpression1() == null)
        this.filterCondition.setFirstExpression(exp1);
      else
        this.filterCondition.addNextExpression(JunctionEnum.OR, exp1);
    }

    if(minLvl<maxLvl)
      buildLevelCondition(minLvl, maxLvl);


    if(factionId > 0) 
      buildFactionCondition(RpgBuilder.getFactionsList().get(factionId-1).getTableName());//Minus 1 'cause the element number 0 is 'All

    if(typeId > 0)
      buildTypeCondition(this.model.getSelectedItem().getItemTypesList().get(typeId-1));

    //System.out.println("Condition = "+filterCondition.getConditionString());
    this.model.setQueryCondition(filterCondition);
  }

  private void buildTypeCondition(Type itemType)
  {
    Literal field = new Literal(itemType.getTableField());
    Literal value = new Literal(itemType.getTableFieldValue());
    Expression typeLike = new Expression(field);
    typeLike.addComparatorAndLiteral(ComparatorEnum.LIKE, value);

    if(filterCondition.getExpression1() == null)
      filterCondition = new Condition(typeLike);
    else
      filterCondition.addNextExpression(JunctionEnum.AND, typeLike);
  }

  private void buildFactionCondition(String factionName)
  {
    Literal faction = new Literal("race_permitted");
    Expression factionLike = new Expression(faction);
    factionLike.addComparatorAndLiteral(ComparatorEnum.LIKE, new Literal(factionName));
    if(filterCondition.getExpression1() == null)
      filterCondition = new Condition(factionLike);
    else
      filterCondition.addNextExpression(JunctionEnum.AND, factionLike);
  }

  private void buildLevelCondition(int minLvl, int maxLvl)
  {
    Literal level = new Literal("level");
    Expression levelGTE = new Expression(level);
    levelGTE.addComparatorAndLiteral(ComparatorEnum.GTE, new Literal(minLvl));
    Expression levelLTE = new Expression(level);
    levelLTE.addComparatorAndLiteral(ComparatorEnum.LTE, new Literal(maxLvl));

    if(filterCondition.getExpression1() == null)
      filterCondition = new Condition(levelGTE);
    else
      filterCondition.addNextExpression(JunctionEnum.AND, levelGTE);
    filterCondition.addNextExpression(JunctionEnum.AND, levelLTE);
  }

}
